package com.yxw.bamboo.core.database.sql;

import com.yxw.bamboo.NetWorkMonitorFactory;
import com.yxw.bamboo.api.dao.HostMapper;
import com.yxw.bamboo.core.NetWorkMonitor;
import com.yxw.bamboo.core.NetworkContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLSyntaxErrorException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

@Component
public class Host {

    @Autowired
    HostMapper hostMapper;
    public static final List<NetworkContext> HOST_LIST=new ArrayList<NetworkContext>();
    public static final Map startedHost=new Hashtable<Integer,NetworkContext>();

    private volatile boolean isSearching=false;
    @Autowired
    NetWorkMonitorFactory netWorkMonitorFactory;
    static {
//        HOST_LIST.add(new NetworkContext("mssql",1256,
//                "127.0.0.1",1433,false,0,"java",""));
//        HOST_LIST.add(new NetworkContext("mssql",1258,
//                "127.0.0.1",1433,false,0,"net","proc"));
//
//        //SELECT * FROM [user] where name=@P0 and id=@P1
//        HOST_LIST.add(new NetworkContext
//                ("mysql",1257, "127.0.0.1",3306,false,1,"java",""));
    }

    public static synchronized void add(NetworkContext networkContext)
    {
        HOST_LIST.add(networkContext);
    }
    public static synchronized void remove(NetworkContext networkContext)
    {
        HOST_LIST.remove(networkContext);
    }

    public  void start()
    {

    }
    public void pullHostInfo()
    {
        //防止多个同时查询
        if(isSearching)
        {
            return;

        }
        isSearching=true;
        try {
            Integer maxIndex=0;
            if(Host.HOST_LIST.size()>0)
            {
                NetworkContext hostInfo= Host.HOST_LIST.stream().max((p,p1)->p.getId()-p1.getId()).get();
                if(hostInfo!=null)
                {
                    maxIndex=hostInfo.getId();
                }
            }
            List<NetworkContext> networkContextList= hostMapper.findAllHostInfo(maxIndex);
            if(networkContextList!=null&&networkContextList.size()>0)
            {
                HOST_LIST.addAll(networkContextList);
            }
        }finally {
            isSearching=false;
        }


    }

    public  void refresh()
    {
        if(HOST_LIST.size()>0)
        {
            for (NetworkContext networkContext: HOST_LIST) {
               if(networkContext.getHostStatus()==null||networkContext.getHostStatus()==HostStatus.error)
               {
                   System.out.println("host--"+networkContext.getRemoteHostAddr()+":"+networkContext.getLocalPort()+"---starting...");

                   if(networkContext.getHostStatus()==HostStatus.error)
                   {
                       networkContext.setRestart();
                   }else
                   {
                       networkContext.setStart();
                   }

                   NetWorkMonitor netWorkmsMonitor= netWorkMonitorFactory.createNetWork(networkContext);
                   Thread msThread=new Thread(netWorkmsMonitor);
                   msThread.start();
//                   startedHost.put(networkContext.getId(),networkContext);
//                   System.out.println("host--"+networkContext.getRemoteHostAddr()+":"+networkContext.getLocalPort()+"---started");

               }
            }
        }
    }

}
